-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
27 lines (22 loc) · 830 Bytes
/
Solution.java
File metadata and controls
27 lines (22 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Scanner;
public class MissingNumber {
public static int findMissingNumber(int[] arr, int n) {
int total = (n + 1) * (n + 2) / 2; // Sum of 1 to n+1
for (int num : arr) {
total -= num; // Subtract array elements from total
}
return total;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array (n): ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.print("Enter " + n + " elements of the array: ");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
int missingNumber = findMissingNumber(arr, n);
System.out.println("The missing number is: " + missingNumber);
}
}